#删除原容器
echo "begin stop and delete old mysql......"
docker_name=`docker ps -a | grep -E "mysql-cqj" | awk 'NR>0 {print $2}'`
echo "old_container_name ="
echo "------->${docker_name}"
if [[ "${docker_name[@]}"  == "" ]]; then
    echo "there's no old mysql-cqj container,continue......."
else
    #查询visitor相关的容器id 并且删除
    docker_id=`docker ps -a | grep -E "mysql-cqj" | awk 'NR>0 {print $1}'`
    docker stop ${docker_id} 
    echo "stop mysql-cqj end."
    docker rm ${docker_id}
    echo "rm mysql-cqj ok."
    echo "stop and delete old mysql-cqj container......success!"
fi
echo "====================================================="


# pull mysql images
echo "=======================start pull mysql images=============================="
docker pull mysql
echo "=======================pull end.=============================="

# check path
mysql_path_log="/home/docker/mysql/log"
if [[ ! -d "$mysql_path_log" ]]; then
    echo "mysql log path not exsits,create now."
    mkdir -p ${mysql_path_log}
else
    echo "mysql log path exsits,do not create."
fi

mysql_path_data="/home/docker/mysql/data"
if [[ ! -d "$mysql_path_data" ]]; then
    echo "mysql data path not exsits,create now."
    mkdir -p ${mysql_path_data}
else
    echo "mysql data path exsits,do not create."
fi

mysql_path_conf="/home/docker/mysql/conf"
if [[ ! -d "$mysql_path_conf" ]]; then
    echo "mysql conf path not exsits,create now."
    mkdir -p ${mysql_path_conf}
else
    echo "mysql conf path exsits,do not create."
fi

# copy mysql:/etc/mysql to /home/docker/mysql/conf
docker run -d -p 3307:3307 -e MYSQL_ROOT_PASSWORD=Chenjin1314... --name mysql-cqj mysql:latest
docker cp mysql-cqj:/etc/mysql /home/docker/mysql/conf
echo "copy conf finish,=_=!"

# copy finish,rm old container
docker stop mysql-cqj && docker rm mysql-cqj

#build new mysql-cqj container
echo "=======================start build mysql-cqj container=============================="
docker run -d \
-p 3306:3306 \
--ip 172.20.1.7 \
--network=bridge-chen \
-v /home/docker/mysql/log:/var/log/mysql \
-v /home/docker/mysql/data:/var/lib/mysql \
-v /home/docker/mysql/conf/mysql:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=Chenjin1314... \
--name mysql-cqj \
--privileged=true \
--restart=always \
mysql:latest
echo "=======================build end.=============================="
